package yxy.game.pm2.command.union.boss;

import yxy.cherry.data.script.fomula;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.module.RedisLock;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.UnionBossMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.union.UnionHandle;
import yxy.game.pm2.module.union.UnionHandle.UnionPlayerEntry;
import yxy.game.pm2.module.union.boss.UnionPlayerBoss;
import yxy.game.pm2.module.union.player.UnionPlayer;

/** 联盟BOSS 联盟BOSS购买挑战次数 */
@MessageType(proto = Command.UnionBossChanllengePurchase, classtype = UnionBossMessage.UnionBossChanllengePurchaseRequest.class)
public class CMD_UnionBossChanllengePurchase extends Command<UnionBossMessage.UnionBossChanllengePurchaseRequest> {

	@Override
	public void execute(MessageSource messageSource, UnionBossMessage.UnionBossChanllengePurchaseRequest request) {
		String playerCode = messageSource.getTag();

		UnionBossMessage.UnionBossChanllengePurchaseResponse.Builder response = UnionBossMessage.UnionBossChanllengePurchaseResponse.newBuilder();

		RedisLock pLock = UnionPlayer.lock(playerCode);
		try {
			if (pLock.tryLock()) {
				// 检查是否已经有联盟
				UnionPlayerEntry entry = new UnionPlayerEntry(playerCode);
				Result result = new UnionHandle().checkPlayerUnion(entry);
				if (!result.equals(Result.UnionIn)) {
					send(messageSource, response, result);
					return;
				}
				UnionPlayer unionPlayer = entry.getUnionPlayer();
				UnionPlayerBoss playerBoss = unionPlayer.getBoss();

				if (playerBoss.buytimes() <= 0) {
					send(messageSource, response, Result.UnionBossBuyTimesLack);// 你的购买次数用完了
					return;
				}

				int costNumber = fomula.联盟boss每日购买花费元宝数.intValue(playerBoss.getBuytimes());
				Currency cost = Currency.create(CurrencyType.CT_元宝, 0, costNumber);
				try {
					new CurrencyHandle().cost(playerCode, CCReason.联盟BOSS, cost);
				} catch (CurrencyNotEnoughException e) {
					send(messageSource, response, new Result(Result.CostFail, e.getType().getNumber(), e.getDataId()));// 消费不足
					return;
				}

				playerBoss.setTimes(playerBoss.getTimes() - 1);
				playerBoss.setBuytimes(playerBoss.getBuytimes() + 1);
				unionPlayer.set();

				response.setTimes(playerBoss.times());
				response.setBuytimes(playerBoss.buytimes());
				send(messageSource, response, Result.Success);
			}
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		} finally {
			pLock.unlock();
		}
	}

	private void send(MessageSource messageSource, UnionBossMessage.UnionBossChanllengePurchaseResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}